<?php
namespace Member\Controller;
use Think\Controller;
class IndexController extends BaseController {
	public function logout(){
		unset($_SESSION['member']);
		$this->redirect('Reg');
	}
    public function index(){
		// dump(session());
		$w=M('Warehouse');
        $root_group_id=M("Member")->alias('m')->join("__DEPARTMENT_MEMBER__ d on d.id=m.department_id",'left')->where("username='".session('member')."'")->getField('root_group_id');
		// echo M("Member")->getlastsql();
        $group_id=explode(',',$root_group_id);
        $root=M('RootMember')->where("is_root=1 or is_root=2")
            ->order("sort")->select();
        $new_arr=array();
        foreach($root as $k=>$v){
            if($v['is_root']==1){
                $new_arr[]=$v;
                unset($root[$k]);
            }
        }
        foreach($new_arr as $k=>$v){
            foreach($root as $j=>$u){
                if($u['controller']==$v['controller']){
                    if(in_array($u['id'],$group_id) || session('type')==1){
                        $new_arr[$k]['child'][]=$u;
                        unset($root[$j]);
                    }
                }
            }
            if(count($new_arr[$k]['child'])==0){
                unset($new_arr[$k]);
            }
        }
		$member_id=session('member.id');
		$warehouse_list=$w->where(array('member_id'=>$member_id))->select();
		
		 $this->assign('warehouse_list',$warehouse_list);
        $this->assign('list',$new_arr);
        $this->display();
    }
}